package com.longge;

public class ReverseWords {

    public String reverseWords(String s) {

        String[] strs = s.split(" ");

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < strs.length; i++) {
            char[] chars = reverseString(strs[i].toCharArray());
            sb.append(chars);
            if (i!=strs.length-1){
                sb.append(' ');
            }
        }
        return sb.toString();

    }

    private char[] reverseString(char[] s) {

        int p1 = 0, p2 = s.length - 1;
        while (p1 < p2) {
            char temp = s[p1];
            s[p1++] = s[p2];
            s[p2--] = temp;
        }
        return s;
    }
}
